package cn.jervain.nut.auth.demo.configuration

import cn.jervain.nut.auth.core.AuthFilter
import cn.jervain.nut.auth.core.provider.AuthProvider
import cn.jervain.nut.auth.core.encoder.PasswordEncoder
import cn.jervain.nut.auth.core.encoder.bcrypt.BCryptPasswordEncoder
import cn.jervain.nut.auth.core.provider.InMemoryAuthProvider
import cn.jervain.nut.auth.core.storage.InMemoryTokenStorage
import cn.jervain.nut.auth.core.storage.TokenStorage
import cn.jervain.nut.auth.demo.bean.User
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
class WebMvcConfiguration: WebMvcConfigurer {

    override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(authFilter())
    }

    @Bean
    fun authFilter() = AuthFilter()

    @Bean
    fun passwordEncoder(): PasswordEncoder = BCryptPasswordEncoder()

    @Bean
    fun authProvider(): AuthProvider = InMemoryAuthProvider(User::class.java)

    @Bean
    fun tokenStorage(): TokenStorage = InMemoryTokenStorage()

}